package com.xinlu.user.action;

import com.xinlu.derek.common.BaseAction;
import com.xinlu.derek.common.BaseForm;
import com.xinlu.persistence.Users;
import com.xinlu.user.bo.UsersBo;
import com.xinlu.user.dao.MD5Util;
import com.xinlu.user.form.AqyxForm;
import com.xinlu.util.tool.CheckUtils;

public class AqyxAction extends BaseAction {

	private UsersBo usersBo;

	AqyxForm fr = new AqyxForm();

	public void setUsersBo(UsersBo usersBo) {
		this.usersBo = usersBo;
	}

	public void setFr(AqyxForm fr) {
		this.fr = fr;
	}

	@Override
	public String doExecute() throws Exception {
		return null;
	}

	@Override
	public void formInit() {
	}

	@Override
	public BaseForm getModel() {
		return fr;
	}

	/**
	 * 加载页面
	 * 
	 * @return
	 */
	public String loadPage() {

		return "aqyx";
	}

	/**
	 * 修改邮箱
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateEmail() throws Exception {

		Users crtUser = (Users) getSession().getAttribute("userinfo");
		crtUser = this.usersBo.loadUser(crtUser.getUsername());

		if (!MD5Util.encodePassword(fr.getDlmm()).toLowerCase().equals(crtUser.getUserpass().toLowerCase())) {
			throw new Exception("登陆密码错误");
		}
		if (!MD5Util.encodePassword(fr.getQkmm()).toLowerCase().equals(crtUser.getUserpass1().toLowerCase())) {
			throw new Exception("取款密码错误");
		}
		
		if (CheckUtils.checkEmail(fr.getAqyx())) {
			
			crtUser.setEmail(fr.getAqyx());
			
			usersBo.update(crtUser);
			getSession().setAttribute("userinfo",crtUser);
		}else{
			
			throw new Exception("安全邮箱不符合格式!");
		}

		return "operSuccess";
	}

}
